package com.zx.viewlearn.views;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.BounceInterpolator;

import com.zx.viewlearn.evaluator.ColorEvaluator;
import com.zx.viewlearn.evaluator.PointEvaluator;
import com.zx.viewlearn.model.Point;

/**
 * Created by zhoux on 2018/1/5.
 * 注释：animator高级用法
 */

public class MyAnimView extends View {

    public static final float RADIUS = 50f;//圆的半径

    private Point currentPoint;//自定义属性，用该属性执行动画

    private Paint mPaint;//画笔


    public MyAnimView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//用于绘制时抗锯齿
        mPaint.setColor(Color.BLUE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (currentPoint == null) {
            currentPoint = new Point(RADIUS, RADIUS, RADIUS);
            drawCircle(canvas);//初始化：画一个圆
            startAnimation();//开始动画效果
        } else {
            drawCircle(canvas);//重新画圆
        }
    }

    /**
     * 画一个圆
     * * @param canvas
     */
    private void drawCircle(Canvas canvas) {
        Log.e("MyViewBall", "getWidth:" + getWidth());
        Log.e("MyViewBall", "getHeight:" + getHeight());
        Log.e("MyViewBall", "mPointSun.getX():" + currentPoint.getX());
        Log.e("MyViewBall", "mPointSun.getY():" + currentPoint.getY());
        float x = currentPoint.getX();//圆心x坐标
        float y = currentPoint.getY();//圆心y坐标
        canvas.drawCircle(x, y, RADIUS, mPaint);//画圆
    }

    private void startAnimation() {
        //
//        Point startPoint = new Point(RADIUS, RADIUS);//开始圆心位置
//        Point endPoint = new Point(getWidth() - RADIUS, getHeight() - RADIUS);//结束圆心位置,
        Point startPoint = new Point(getWidth() / 2, RADIUS, RADIUS);
        Point endPoint = new Point(getWidth() / 2, getHeight() - RADIUS, RADIUS);
        ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);//传入自定义即TypeEvaluator,和自定义属性的初始值和结束值
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentPoint = (Point) animation.getAnimatedValue();//每次动画更新，即TypeEvaluator的evaluate方法被执行，重新给自定义属性赋值
                invalidate();//重新绘制，会调用onDraw()
            }
        });
        anim.setInterpolator(new BounceInterpolator());//设置插值器，自由落体，且会弹起
        anim.setDuration(2000);//1秒执行时间
        anim.start();
    }

    private void startAnimation2() {
        Point startPoint = new Point(getWidth() / 2, RADIUS, RADIUS);
        Point endPoint = new Point(getWidth() / 2, getHeight() - RADIUS, RADIUS);
        ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);
        anim.setInterpolator(new BounceInterpolator());//设置插值器
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentPoint = (Point) animation.getAnimatedValue();
                invalidate();
            }
        });
        ObjectAnimator anim2 = ObjectAnimator.ofObject(this, "color", new ColorEvaluator(),
                "#0000FF", "#FF0000");//用ColorEvaluator更新当前view的color属性
        AnimatorSet animSet = new AnimatorSet();
        animSet.play(anim).with(anim2);
        animSet.setDuration(3000);
        animSet.start();
    }


    private String color;//动画属性

    public String getColor() {
        return color;
    }

    public void setColor(String color) {//ObjectAnimator.ofObject动画会更新该属性
        this.color = color;
        mPaint.setColor(Color.parseColor(color));
        invalidate();
    }


}
